package front

import (
	"hft/Http/Models/request"
	services "hft/Http/Services"
	"hft/Http/Services/front/clue"
	utils "hft/Http/Utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Clue struct{}

func (*Clue) LeaveClue(c *gin.Context) {

	var data *request.Clue

	c.ShouldBindJSON(&data)

	ip := c.ClientIP()

	data.IP = ip

	city, _ := strconv.Atoi(c.Request.Header.Get("sid"))

	if city == 0 {
		utils.FailWithMessage("当前城市还未加盟", c)
		return
	}

	newData, err := clue.ClueFrontService.LeaveClue(data, city)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(newData, c)
}

func (*Clue) ValidUser(c *gin.Context) {

	var sid, _ = strconv.Atoi(c.Request.Header.Get("sid"))

	var data *request.ValidCode

	c.ShouldBindJSON(&data)

	err := clue.ClueFrontService.ValidCode(*data, sid)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithMessage("验证成功", c)
}

func (*Clue) SendCode(c *gin.Context) {

	var phone *request.SendCode

	c.ShouldBindJSON(&phone)

	err := services.BaseService.SendCode(phone.Phone, "USER_")

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

var ClueController = new(Clue)
